/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author Administrator
 * Date: 2010-7-7 下午04:48:37
 */
package com.ccxe.manage.service.dwr.permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ccxe.manage.dao.impl.IM_Menu_Role_DaoImpl;
import com.ccxe.manage.dao.impl.MenuItemDaoImpl;
import com.ccxe.manage.service.impl.IM_Menu_Role_Service;
import com.ccxe.manage.service.impl.RoleDaoService;
import com.ccxe.manage.util.help.JSON_Convertor;

/**
 * <角色分配权限> 无刷新保存 后台类
 * 
 * @author wangyx
 * 
 */
public class PermissionSaveDwrService {

	private IM_Menu_Role_Service service = new IM_Menu_Role_Service();
	private MenuItemDaoImpl dao = new MenuItemDaoImpl();
	private RoleDaoService daoService = new RoleDaoService();
	private static final Logger logger = LoggerFactory.getLogger(PermissionSaveDwrService.class);
	
	public String saveRole_Permission(String roleId, String permissionBuffer) {
		try {
			String[] permissionIds = permissionBuffer.split(",");
			Map<String, Object> parameterMap = new HashMap<String, Object>();
			logger.info("RoleID : "+roleId.trim());
			parameterMap.put("roleId", roleId.trim());
			parameterMap.put("menuList", permissionIds);
			service.setParameterMap(parameterMap);
			service.processTransaction();
		} catch (Exception e) {
			return "false";
		}
		return "true";
	}

	public String getTreeByRoleId(String roleId) {
		List<Map<?, ?>> list = null;
		try {
			list = dao.queryAllByRole(roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSON_Convertor convertor = new JSON_Convertor();
		return convertor.convertTreeListToJson(list);
	}

	public String viewExitsPermissionByRole(String roleId) {
		List<Map<?, ?>> list = null;
		try {
			IM_Menu_Role_DaoImpl imrDao = new IM_Menu_Role_DaoImpl();
			list = imrDao.queryListByRoleID(roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSON_Convertor convertor = new JSON_Convertor();
		return convertor.convertTreeListToJson(list);
	}

	public String saveRole(String id, String name, String des) {
		// 添加--保存操作
		int i = daoService.insertOne(new String[] { id, name, des });
		return i == 0 ? "failure" : "success";
	}
}
